home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrbackbuffer / vrbackbuffer.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.0 KB  |  254 lines

  1. //////////
  2. //
  3. //    File:        VRBackBuffer.c
  4. //
  5. //    Contains:    Sample code showing how to work with orientation of QuickTime VR back buffer.
  6. //
  7. //    Written by:    Ken Doyle
  8. //    Revised by: Tim Monroe
  9. //
  10. //    Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <1>         06/26/99    rtm        first file 
  15. //       
  16. //    This file contains functions that illustrate how to determine whether the back buffer is oriented
  17. //    vertically or horizontally, and how to draw a picture accordingly.
  18. //
  19. //////////
  20.  
  21. //////////
  22. //       
  23. // header files
  24. //       
  25. //////////
  26.  
  27. #include "VRBackBuffer.h"
  28.  
  29.  
  30. //////////
  31. //
  32. // VRBB_InitWindowData
  33. // Initialize any window-specific data.
  34. //
  35. //////////
  36.  
  37. ApplicationDataHdl VRBB_InitWindowData (WindowObject theWindowObject)
  38. {
  39. #pragma unused(theWindowObject)
  40.  
  41.     ApplicationDataHdl    myAppData;
  42.     
  43.     myAppData = (ApplicationDataHdl)NewHandleClear(sizeof(ApplicationDataRecord));
  44.     if (myAppData != NULL) {
  45.         (**myAppData).fPicture = NULL;
  46.         (**myAppData).fPictureWidth = kDefaultEmbPictWidth;
  47.         (**myAppData).fBackBufferProc = NewQTVRBackBufferImagingProc(VRBB_BackBufferImagingProc);
  48.         (**myAppData).fCurrMenuItem = 0;
  49.     }
  50.  
  51.     return(myAppData);
  52. }
  53.  
  54.  
  55. //////////
  56. //
  57. // VRBB_DumpWindowData
  58. // Dispose of any window-specific data.
  59. //
  60. //////////
  61.  
  62. void VRBB_DumpWindowData (WindowObject theWindowObject)
  63. {
  64.     ApplicationDataHdl        myAppData = NULL;
  65.  
  66.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  67.     if (myAppData == NULL)
  68.         return;
  69.  
  70.     // if we have a lingering picture, dump it
  71.     if ((**myAppData).fPicture != NULL) {
  72.         KillPicture ((**myAppData).fPicture);
  73.         (**myAppData).fPicture = NULL;
  74.     }
  75.     
  76.     DisposeRoutineDescriptor((**myAppData).fBackBufferProc);
  77. }
  78.     
  79.     
  80. //////////
  81. //
  82. // VRBB_InstallBackBufferImagingProc
  83. // Install a back buffer imaging procedure.
  84. //
  85. //////////
  86.  
  87. OSErr VRBB_InstallBackBufferImagingProc (QTVRInstance theInstance, WindowObject theWindowObject, short thePictResID, Boolean theIsHoriz)
  88. {
  89.     ApplicationDataHdl        myAppData;
  90.     QTVRAreaOfInterest        myArea;
  91.     float                    myWidth, myHeight;
  92.     Rect                    myPictRect;
  93.     OSErr                    myErr = noErr;
  94.  
  95.     // initialize; clean up any existing back buffer procedure
  96.     if ((theInstance == NULL) || (theWindowObject == NULL)) 
  97.         return(paramErr);
  98.  
  99.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  100.     if (myAppData == NULL) 
  101.         return(paramErr);
  102.  
  103.     HLock((Handle)myAppData);
  104.     
  105.     VRBB_CleanUpBackBuffer(theWindowObject);
  106.  
  107.     // get the picture to embed    
  108.     (**myAppData).fPicture = GetPicture(thePictResID);
  109.     if ((**myAppData).fPicture == NULL) {
  110.         VRBB_CleanUpBackBuffer(theWindowObject);
  111.         goto bail;
  112.     }
  113.  
  114.     DetachResource((Handle)(**myAppData).fPicture);
  115.     
  116.     myPictRect = (*(**myAppData).fPicture)->picFrame;
  117. #ifdef TARGET_OS_WIN32
  118.     myPictRect.top = EndianU16_BtoN(myPictRect.top);
  119.     myPictRect.left = EndianU16_BtoN(myPictRect.left);
  120.     myPictRect.bottom = EndianU16_BtoN(myPictRect.bottom);
  121.     myPictRect.right = EndianU16_BtoN(myPictRect.right);
  122. #endif
  123.     
  124.     MacOffsetRect(&myPictRect, -myPictRect.left, -myPictRect.top);
  125.  
  126.     // set the area of interest    
  127.     myWidth = kDefaultEmbPictWidth;
  128.     
  129.     if (theIsHoriz)
  130.         myHeight = myWidth * (((float)myPictRect.bottom) / ((float)myPictRect.right));
  131.     else
  132.         myHeight = myWidth * (((float)myPictRect.right) / ((float)myPictRect.bottom));
  133.  
  134.     // center the picture on the current pan and tilt angles
  135.     myArea.panAngle = QTVRGetPanAngle(theInstance) + (myWidth/2);
  136.     myArea.tiltAngle = QTVRGetTiltAngle(theInstance) + (myHeight/2);
  137.  
  138.     myArea.width = myWidth;
  139.     myArea.height = myHeight;
  140.  
  141.     // if the back buffer is oriented horizontally, set the appropriate flag
  142.     myArea.flags = theIsHoriz ? kQTVRBackBufferHorizontal : 0;
  143.  
  144.     // install our procedure
  145.     myErr = QTVRSetBackBufferImagingProc(theInstance, (**myAppData).fBackBufferProc, 1, &myArea, (SInt32)theWindowObject);
  146.  
  147. bail:
  148.     HUnlock((Handle)myAppData);
  149.     return(myErr);
  150. }
  151.     
  152.  
  153. //////////
  154. //
  155. // VRBB_CleanUpBackBuffer
  156. //
  157. //////////
  158.  
  159. OSErr VRBB_CleanUpBackBuffer (WindowObject theWindowObject)
  160. {
  161.     ApplicationDataHdl        myAppData;
  162.     OSErr                    myErr = paramErr;
  163.  
  164.     if (theWindowObject == NULL) 
  165.         goto bail;
  166.     
  167.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  168.     if (myAppData == NULL)
  169.         goto bail;
  170.     
  171.     // if we have a lingering picture, dump it
  172.     if ((**myAppData).fPicture != NULL) {
  173.         KillPicture ((**myAppData).fPicture);
  174.         (**myAppData).fPicture = NULL;
  175.     }
  176.     
  177.     // clear any back-buffer procedure
  178.     myErr = QTVRSetBackBufferImagingProc((**theWindowObject).fInstance, NULL, 0, NULL, 0);
  179.  
  180.     // make sure the back buffer is clean
  181.     myErr = QTVRRefreshBackBuffer((**theWindowObject).fInstance, 0);
  182.     
  183. bail:
  184.     return(myErr);
  185. }
  186.  
  187.  
  188. //////////
  189. //
  190. // VRBB_BackBufferImagingProc
  191. // The back buffer imaging procedure: draw the current picture into the back buffer.
  192. //
  193. //////////
  194.  
  195. PASCAL_RTN OSErr VRBB_BackBufferImagingProc (QTVRInstance theInstance, Rect *theRect, UInt16 theAreaIndex, UInt32 theFlagsIn, UInt32 *theFlagsOut, WindowObject theWindowObject)
  196. {
  197. #pragma unused(theAreaIndex)
  198.  
  199.     ApplicationDataHdl        myAppData = NULL;
  200.     Boolean                    myIsDrawing = theFlagsIn & kQTVRBackBufferRectVisible;
  201.     OSErr                    myErr = paramErr;
  202.     
  203.     // assume we're not going to draw anything
  204.     *theFlagsOut = 0;
  205.     
  206.     if ((theInstance == NULL) || (theWindowObject == NULL)) 
  207.         goto bail;
  208.  
  209.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  210.     if (myAppData == NULL) 
  211.         goto bail;
  212.  
  213.     if (myIsDrawing) {
  214.         if ((**myAppData).fPicture != NULL) {
  215.             DrawPicture((**myAppData).fPicture, theRect);
  216.             *theFlagsOut = kQTVRBackBufferFlagDidDraw;        // if we drew something, tell QuickTime VR
  217.         }
  218.     }
  219.     
  220.     myErr = noErr;
  221.     
  222. bail:    
  223.     return(myErr);
  224. }
  225.  
  226.  
  227. //////////
  228. //
  229. // VRBB_IsBackBufferHorizontal
  230. // Is the back buffer oriented horizontally?
  231. //
  232. //////////
  233.  
  234. Boolean VRBB_IsBackBufferHorizontal (QTVRInstance theInstance)
  235. {
  236.     UInt32        myGeometry;
  237.     UInt16        myResolution;
  238.     UInt32        myCachePixelFormat;
  239.     SInt16        myCacheSize;
  240.     OSErr        myErr = noErr;
  241.  
  242.     if (theInstance == NULL)
  243.         return(false);
  244.  
  245.     myErr = QTVRGetBackBufferSettings(theInstance, &myGeometry, &myResolution, &myCachePixelFormat, &myCacheSize);
  246.     if (myErr != noErr)
  247.         return(false);
  248.     else
  249.         return(myGeometry == kQTVRHorizontalCylinder);
  250. }
  251.  
  252.  
  253.  
  254.